package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ListHelper;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

/**
 * 虚拟机相关接口
 * 
 * @author dmw
 *
 */
@RestController
@RequestMapping("/vserver")
public class VirtualServerController {
	private static Logger logger = LoggerFactory.getLogger(VirtualServerController.class);
	private static final String BASE_SERVICE = "/pool";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 获取资源池列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"}]
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回资源池的详细信息，为true时只返回id和name值。
	 * @return 数据结果集
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		Map<String, Object> param = ListHelper.assembleParam(page, rows, params, sorter, simple);
		String url = BASE_SERVICE + "/list";
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LIST, null, param, request);
		return service.invoke();
	}

	/**
	 * 添加资源池
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 添加结果
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/create";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			logger.info(JSONObject.toJSONString(service));
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 修改资源池
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject metas = JSONObject.parseObject(params);
		if (null != metas && metas.containsKey(Common.ID)) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			String url = UrlTranslator.translate(BASE_SERVICE + "/modify", metas.getLong(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.MODIFY, null, paramMap,
					request);
			return service.invoke();
		} else {
			logger.warn("参数格式异常！");
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 移除资源池
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/remove", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.REMOVE, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看资源池详细信息
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/detail", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看资源池基本信息
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/basic", method = { RequestMethod.GET })
	public BsmResult basic(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/basic", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 获取资源池资源列表
	 * 
	 * @param id
	 *            资源池ID
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"}]
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回集群的详细信息，为true时只返回id和name值
	 * @param type
	 *            资源类型
	 * @return 数据结果集
	 */
	@RequestMapping(value = "/resources", method = { RequestMethod.GET })
	public BsmResult resources(@RequestParam(value = Common.ID, required = false) Long id,
			@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple,
			@RequestParam(value = Common.TYPE, required = false) String type, HttpServletRequest request) {
		Map<String, Object> param = ListHelper.assembleParam(page, rows, params, sorter, simple);
		if (null != type) {
			param.put(Common.TYPE, type);
		}
		if (null != id) {
			param.put(Common.ID, id);
		} else {
			logger.warn("参数格式异常！");
			return ResultTools.formatErrResult();
		}
		String url = UrlTranslator.translate(BASE_SERVICE + "/resources", id);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LIST, null, param, request);
		return service.invoke();
	}

	/**
	 * 添加资源
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/addRes", method = { RequestMethod.POST })
	public BsmResult addRes(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.POOL)) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, jsonObject.get(Common.PARAMS));
			String url = UrlTranslator.translate(BASE_SERVICE + "/addRes", jsonObject.get(Common.POOL));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 移除资源
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/delRes", method = { RequestMethod.POST })
	public BsmResult delRes(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID) && jsonObject.containsKey(Common.POOL)) {
			Map<String, Object> paramMap = MapTools.simpleMap(Common.RESOURCE, jsonObject.get(Common.ID));
			String url = UrlTranslator.translate(BASE_SERVICE + "/delRes", jsonObject.get(Common.POOL));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.REMOVE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查看资源池的负载信息
	 * 
	 * @param params
	 *            资源池属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @return 操作结果
	 */
	@RequestMapping(value = "/load", method = { RequestMethod.GET })
	public BsmResult load(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			String url = UrlTranslator.translate(BASE_SERVICE + "/load", jsonObject.get(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LOAD, null, null, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}
}
